google.load("search", "1");

var QueryString = "";

var SelectedCandidate = "";
var SelectedCandidateParty = "";
var SelectedIssue = "";
var includeParty = false;
var includePolitics = false;
var issueList = new Array();
var issueCounts = new Object();
var issueCallBackCount = 0;
var issueWebSearches = new Object();

// Call this function when the page has been loaded
function OnLoad() {
	// create a tabbed mode search control
	var searchControl = new google.search.SearchControl();
	searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	// add searches
	searchControl.addSearcher(new google.search.WebSearch());
	searchControl.addSearcher(new google.search.NewsSearch());
	searchControl.addSearcher(new google.search.BlogSearch());
	searchControl.addSearcher(new google.search.ImageSearch());
	searchControl.addSearcher(new google.search.VideoSearch());
	// draw in tabbed layout mode
	var drawOptions = new google.search.DrawOptions();
	drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
	drawOptions.setSearchFormRoot(document.getElementById("searchcontrol"));
	searchControl
			.draw(document.getElementById("containerResults"), drawOptions);

	//searchControl.setSearchCompleteCallback(this, refreshQueryReturnCount);
	var searchForm = new google.search.SearchForm(true, document
			.getElementById('searchcontrol'));
	searchForm.setOnSubmitCallback(searchControl, function() {
		//window.alert(searchForm.input.value);
			this.execute(searchForm.input.value);
			//refreshQueryReturnCount();
		});
	searchForm.setOnClearCallback(searchControl, function() {
		SelectedCandidate = '';
		SelectedIssue = '';
		searchForm.input.value = '';
		//window.alert(SelectedIssue + " " + SelectedCandidate);
			searchControl.clearAllResults();
			//fadeFermer('recherche');
		});
	searchForm.execute(QueryString);
}
google.setOnLoadCallback(OnLoad);

function newQuery() {
	QueryString = (SelectedCandidate?((includePolitics?"Politics ":"") + SelectedCandidate):"") + 
				  (SelectedIssue?(", " + SelectedIssue):"") +
					((includeParty && SelectedCandidate)?(", \"" + SelectedCandidateParty + "\""):"");
	OnLoad();
}

function queryCandidate(candidate, party) {
	SelectedCandidate = candidate;
	SelectedCandidateParty = party;
	refreshQueryReturnCount();
	newQuery();
}

function queryIssue(issue) {
	SelectedIssue = issue;
	newQuery();
}

function changeIncludePolitics() {
	includePolitics = !includePolitics;
	document.getElementById("includePoliticsLink").innerHTML = 
		(includePolitics?"Add":"Remove") + " key word \"Politics\""
	newQuery();
}

function changeIncludeParty() {
	includeParty = !includeParty;
	document.getElementById("includePartyLink").innerHTML = 
		(includeParty?"Exclude":"Include") + " Candidate's Party"
	newQuery();
}

function resultCountCallback(query) {
	//document.write(query + "<br/>");
	issueCounts[query] = issueWebSearches[query].cursor.estimatedResultCount;
	issueCallBackCount ++;
	count = issueWebSearches[query].cursor.estimatedResultCount;
	if (count > 1000000) {
		countString = count / 1000000 + "M";
	}
	else if (count > 1000) {
		countString = count / 1000 + "K";
	}
	else {
		countString = count;
	}
	document.getElementById("issue-link-" + query).innerHTML = query + " (" + countString + ")";

	if (issueCallBackCount == issueList.length) {
		sortIssueByName();
	}
}
function getQueryReturnCount(query) {
	websearch = new google.search.WebSearch();
	issueWebSearches[query] = websearch;
	websearch.setSearchCompleteCallback(this, resultCountCallback, new Array(
			query));
	SelectedCandidate? websearch.execute(SelectedCandidate + " " + query) : websearch.execute(query);
}

function refreshQueryReturnCount() {
	issueCallBackCount = 0;
	webSearch = new google.search.WebSearch();
	//document.write(issueList.length + "<br/>");
	for (var i = 0; i < issueList.length; ++i) {
		//document.write(issueList[i] + "<br/>");
		getQueryReturnCount(issueList[i]);
	}
}

function sortIssues(value) {
	this[value]();
}

function sortIssueBySearchReturn() {
	issueList.sort(function(a, b) {
		return issueCounts[a] - issueCounts[b];
	});
	issueList.reverse();
	renderIssueList();
}

function sortIssueByName() {
	issueList.sort();
	renderIssueList();
}

function renderIssueList() {
	var issueTree = document.getElementById("IssueTree");
	//var issueOrder = "";
	for (var i = 0; i < issueList.length; i++) {
		//issueOrder += issueList[i] + ">>"+ issueCounts[issueList[i]] + "<br/>";
		issueTree.appendChild(document.getElementById("issue-" + issueList[i]));
	}
	/*
	var newdiv=document.createElement("div")
	var newtext=document.createTextNode(issueOrder)
	newdiv.appendChild(newtext) //append text to new div
	document.body.appendChild(newdiv)*/
}